package com.readbook.action;


import com.alibaba.fastjson.JSONArray;
import com.readbook.service.JfychService;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/jfych")
public class jfych {
    @Resource
    private JfychService jfychservice;

    /**
     * @author 牟明艳
     * @comments 查询积分与称号
     * @time 2019年8月14日10:04:46
     */
    @RequestMapping("getJfych.html")
    @ResponseBody
    public List<Map<String, Object>> getJfych(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> list1 = jfychservice.getJfych();
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 添加积分与称号
     * @time 2019年8月13日13:58:25
     */
    @RequestMapping("saveJfych.html")
    public @ResponseBody
    Map<String, Object> saveJfych(HttpServletRequest req) throws Exception {
        Map<String, Object> rmap = new HashMap<String, Object>();
        String params = req.getParameter("params").toString().trim();
        //先删除所有的积分与称号
        jfychservice.del_Jfych();
        List<Map<String, Object>> listr = (List<Map<String, Object>>) JSONArray.parse(params);
        for (int k = 0; k < listr.size(); k++) {
            Map<String, Object> maps = listr.get(k);
            int m = jfychservice.add_Jfych(maps);
            if (m > 0) {
                continue;
            } else {
                rmap.put("result", false);
                rmap.put("msg", "添加失败,请联系管理员");
                return rmap;
            }
        }
        rmap.put("result", true);
        rmap.put("msg", "保存成功!");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 查询首页轮播图图片
     * @time 2019年8月15日16:50:33
     */
    @RequestMapping("getlunboPic.html")
    @ResponseBody
    public List<Map<String, Object>> getlunboPic(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("code", "lunbo");
        List<Map<String, Object>> list1 = jfychservice.getlunboPic(rmap);
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 添加首页轮播图图片
     * @time 2019年8月15日16:51:09
     */
    @RequestMapping("insertLunboPic.html")
    public @ResponseBody
    Map<String, Object> insertLunboPic(HttpServletRequest req) throws Exception {
        Map<String, Object> rmap = new HashMap<String, Object>();
        String photos = req.getParameter("photos").toString().trim();
        //先删除所有首页轮播图
        rmap.put("code", "lunbo");
        jfychservice.delLunboPic(rmap);

        List<String> listr = (List<String>) JSONArray.parse(photos);
        for (int k = 0; k < listr.size(); k++) {
            String picurl = listr.get(k);
            Map<String, Object> maps = new HashMap<String, Object>();
            maps.put("picurl", picurl);
            maps.put("code", "lunbo");
            int m = jfychservice.add_picture(maps);
            if (m > 0) {
                continue;
            } else {
                rmap.put("result", false);
                rmap.put("msg", "添加失败,请联系管理员");
                return rmap;
            }
        }

        //删除不是封面的图片
        String fileurls = "upload" + "/bookfm";
        String filepath = req.getServletContext().getRealPath(fileurls);
        File file = new File(filepath);
        if (file.isDirectory()) {
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                String filed = fileurls + "/" + filelist[i];
                boolean del = true;
                for (int k = 0; k < listr.size(); k++) {
                    String picurl = listr.get(k);
                    if (picurl.equals(filed)) {
                        del = false;
                    }
                }
                if (del) {
                    File readfile = new File(filepath + "/" + filelist[i]);
                    readfile.delete();
                }
            }
        }
        rmap.put("result", true);
        rmap.put("msg", "保存成功!");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 查询顶部图片
     * @time 2019年10月18日23:10:32
     */
    @RequestMapping("getTopImagePic.html")
    @ResponseBody
    public Map<String, Object> getTopImagePic(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("code", "hxtdtop");
        List<Map<String, Object>> list2 = jfychservice.getlunboPic(rmap);
        if (list2.size() > 0) {
            rmap.put("photos", list2.get(0).get("PICURL").toString());
        }
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 添加顶部图片
     * @time 2019年10月18日23:10:32
     */
    @RequestMapping("InsertTopImagePic.html")
    public @ResponseBody
    Map<String, Object> InsertTopImagePic(HttpServletRequest req) throws Exception {
        Map<String, Object> rmap = new HashMap<String, Object>();
        //环西天地顶部图片
        String photos = "";
        if (req.getParameter("photos") != null) {
            photos = req.getParameter("photos").toString().trim();
            rmap.put("code", "hxtdtop");
            jfychservice.delLunboPic(rmap);

            Map<String, Object> hxtd = new HashMap<String, Object>();
            hxtd.put("picurl", photos);
            hxtd.put("code", "hxtdtop");
            int n = jfychservice.add_picture(hxtd);
        }

        String fileurls = "upload" + "/hxtdtop";
        String filepath = req.getServletContext().getRealPath(fileurls);
        File file = new File(filepath);
        if (file.isDirectory()) {
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                String filed = fileurls + "/" + filelist[i];
                if (!photos.equals(filed)) {
                    File readfile = new File(filepath + "/" + filelist[i]);
                    readfile.delete();
                }
            }
        }

        rmap.put("result", true);
        rmap.put("msg", "保存成功!");
        return rmap;
    }


}
